/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.version1.diabeco.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.version1.diabeco.R;
import com.version1.diabeco.model.glucosa.Glucosa;
import com.version1.diabeco.ui.glucosa.ActivityRegistroGlucosa;
// Need the following import to get access to the app resources, since this
// class is in a sub-package.

/**
 * This is an example of implement an {@link BroadcastReceiver} for an alarm that should occur once.
 * <p>
 * When the alarm goes off, we show a <i>Toast</i>, a quick message.
 */
public class AlarmaRegistroGlucosa extends BroadcastReceiver {
	private NotificationManager notificationManager;

	public static final int GLUCOSA_ALARM_REQUEST_CODE = 235123;

	private static int MOOD_NOTIFICATIONS = R.layout.activity_ayuda;

	@Override
	public void onReceive(Context context, Intent intent) {
		notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

		String title = (String) intent.getExtras().get(AlarmaService.BROADCAST_TEXT);

		Intent alarmIntent = new Intent(context, ActivityRegistroGlucosa.class);
		alarmIntent.putExtra(ActivityRegistroGlucosa.INTENT_VALUE_GLUCOSA, new Glucosa());
		PendingIntent pendingIntent = PendingIntent.getActivity(context, GLUCOSA_ALARM_REQUEST_CODE, alarmIntent,
				PendingIntent.FLAG_CANCEL_CURRENT);
		this.showNotification(context, R.drawable.ic_alarma, title, "Debes ingresar tu glucosa!", pendingIntent);

	}

	private void showNotification(Context context, int imageId, String title, String text, PendingIntent contentIntent) {

		// Set the icon, scrolling text and timestamp.
		// Note that in this example, we pass null for tickerText. We update the icon enough that
		// it is distracting to show the ticker text every time it changes. We strongly suggest
		// that you do this as well. (Think of of the "New hardware found" or "Network connection
		// changed" messages that always pop up)
		Notification notification = new Notification(imageId, null, System.currentTimeMillis());

		// Set the info for the views that show in the notification panel.
		notification.setLatestEventInfo(context, title, text, contentIntent);

		// notification.flags = Notification.FLAG_INSISTENT | Notification.FLAG_AUTO_CANCEL;
		notification.flags |= Notification.FLAG_AUTO_CANCEL;

		notification.defaults |= Notification.DEFAULT_SOUND;
		notification.defaults |= Notification.DEFAULT_VIBRATE;

		// Send the notification.
		// We use a layout id because it is a unique number. We use it later to cancel.
		notificationManager.notify(MOOD_NOTIFICATIONS, notification);

	}

}
